﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manage.Model;

namespace Manage.Basic
{
    public partial class FrmAuthMgr : Form
    {
        public FrmAuthMgr()
        {
            InitializeComponent();
        }
        private bool init;//判断节点处理是否初始化完成

        private void FrmAuthMgr_Load(object sender, EventArgs e)
        {
            try
            {
                init = true;
                //获取用户列表
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("姓名");
                RetModel ret = Share.PostData("51API/Manager/GetManagerList", dc, null);
                if (ret.Success)
                {
                    ManagerModel[] mm = JsonConvert.DeserializeObject<ManagerModel[]>(ret.Data.ToString());
                    for (int i = 0; i < mm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = mm[i].id;
                        dr["姓名"] = mm[i].MName;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns[0].Visible = false;
                }

                //获取权限列表
                GetAuthList();
                init = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void GetAuthList()
        {
            try
            {
                //获取权限列表tree
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AuthListModel[] aList = null;
                RetModel ret = Share.PostData("51API/Auth/GetAuthList", dc, null);
                if (ret.Success)
                {
                    aList = JsonConvert.DeserializeObject<AuthListModel[]>(ret.Data.ToString());
                }
                else
                {
                    throw new Exception(ret.ErrInfo);
                }
                //循环类别根节点
                for (int i = 0; i < aList.Length; i++)
                {
                    TreeNode tn = new TreeNode()
                    {
                        Name = aList[i].FCate,
                        Text = aList[i].FCate,
                        Tag = ""
                    };
                    //循环子节点
                    for (int j = 0; j < aList[i].FAuth.Length; j++)
                    {
                        TreeNode ctn = new TreeNode()
                        {

                            Text = aList[i].FAuth[j].FDescription,
                            Name = aList[i].FAuth[j].Id.ToString(),
                            Tag = aList[i].FAuth[j].Id.ToString()
                        };
                        tn.Nodes.Add(ctn);
                    }
                    treeView1.Nodes.Add(tn);
                }
                treeView1.ExpandAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                init = true;
                AuthorModel[] author = null;
                //清空选中
                for (int i = 0; i < treeView1.Nodes.Count; i++)
                {
                    treeView1.Nodes[i].Checked = false;
                    for (int j = 0; j < treeView1.Nodes[i].Nodes.Count; j++)
                    {
                        treeView1.Nodes[i].Nodes[j].Checked = false;
                    }
                }
                Dictionary<string, string> dc = new Dictionary<string, string>();

                int id = Convert.ToInt32(gridView1.GetFocusedDataRow()["id"]);


                dc.Add("id", id.ToString());
                dc.Add("Token", Share.MoM.Token);
                RetModel ret = Share.PostData("51API/Auth/GetAuthorList", dc, null);
                if (ret.Success)
                {
                    if (null == ret.Data)
                    {
                        init = false;
                        return;
                    }

                    author = JsonConvert.DeserializeObject<AuthorModel[]>(ret.Data.ToString());
                }
                else
                {
                    throw new Exception(ret.ErrInfo);
                }

                //勾选
                for (int i = 0; i < author.Length; i++)
                {

                    TreeNode[] tn = treeView1.Nodes.Find(author[i].Aid.ToString(), true);
                    if (tn != null && tn.Length > 0)
                        tn[0].Checked = true;
                }
                //根节点判断
                CheckRootNode();
                init = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.SelectedRowsCount < 1)
                {
                    MessageBox.Show("必须选择要授权的操作员!");
                    return;
                }
                //选中用户和当前用户相同 不可修改权限
                if (Share.MoM.id == Convert.ToInt32(gridView1.GetFocusedDataRow()["id"]))
                {
                    throw new Exception("不能授权自己！");
                }
                List<AuthorModel> authorList = new List<AuthorModel>();
                //保存节点信息
                for (int i = 0; i < treeView1.Nodes.Count; i++)
                {
                    for (int j = 0; j < treeView1.Nodes[i].Nodes.Count; j++)
                    {
                        if (treeView1.Nodes[i].Nodes[j].Checked)
                        {
                            AuthorModel author = new AuthorModel();
                            author.Aid = Convert.ToInt32(treeView1.Nodes[i].Nodes[j].Tag);
                            author.Uid = Convert.ToInt32(gridView1.GetFocusedDataRow()["id"]);
                            authorList.Add(author);
                        }
                    }
                }
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AuthorModel[] aut = authorList.ToArray();
                RetModel ret = Share.PostData("51API/Auth/SaveAuthorInfo", dc, JsonConvert.SerializeObject(aut));
                if (ret.Success)
                {
                    MessageBox.Show("授权成功！");
                    return;
                }
                else
                {
                    MessageBox.Show(ret.ErrInfo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void CheckRootNode()
        {
            try
            {
                init = true;
                //根节点判断
                for (int i = 0; i < treeView1.Nodes.Count; i++)
                {

                    bool isCheck = true;
                    for (int j = 0; j < treeView1.Nodes[i].Nodes.Count; j++)
                    {
                        if (treeView1.Nodes[i].Nodes[j].Checked == false)
                            isCheck = false;
                    }
                    if (isCheck)
                        treeView1.Nodes[i].Checked = true;
                }
                init = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (init)
                    return;
                if (e.Node.Nodes.Count > 0)
                {
                    init = true;
                    for (int i = 0; i < e.Node.Nodes.Count; i++)
                    {
                        e.Node.Nodes[i].Checked = e.Node.Checked;
                    }
                }
                CheckRootNode();
                init = false;
            }
            catch (Exception ex)
            {
                init = false;
                //treeView1.Enabled = true;
                MessageBox.Show(ex.Message);
            }
        }
    }
}
